﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoderJoe
{
    public class ModelView
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public bool IsNew { get; set; }

        public void New()
        {
            Id = 0;
            Name = String.Empty;
            Email = String.Empty;
            IsNew = true;
        }

        public void Load(int id)
        {
            Controller controller = new Controller();
            Model model = controller.Get(id);

            SyncToModel(model);
            IsNew = false;
        }

        private void SyncToModel(Model model)
        {
            Id = model.Id;
            Name = model.Name;
            Email = model.Email;
            IsNew = false;
        }

        public void Save()
        {
            Controller controller = new Controller();
            Model model = new Model();
            model.Id = Id;
            model.Name = Name;
            model.Email = Email;

            if (IsNew)
            {
                model = controller.Create(model);
            }
            else
            {
                model = controller.Update(model);
            }

            SyncToModel(model);
            IsNew = false;
        }
    }
}
